<template>
  <div>
    <!--条件筛选-->
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" labelWidth="110" @filterChange="changeHandle"
               @onCollapse="collapseHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams2"
      isSelectColumn
      datakey="records"
      :onRowSelectChange="selectChange"
      :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>
        <OEMImport @close="closeHandler"/>
        <el-button size="small" type="primary" icon="el-icon-tickets" :loading="loading"
                   :disabled="!rowSelectData.length"
                   @click="visbleDialog = true">批量提报
        </el-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visbleDialog" title="订单批量提报"
                :containerStyle="{ overflow: 'auto', paddingBottom: '60px' }"
                destroyOnClose>
      <batch-submit
        :orders="rowSelectData"
        @closeDialog='e=>{visbleDialog = false;closeHandler(e)}'></batch-submit>
    </BaseDialog>
    <Drawer :visible.sync="visibleDrawer" :title="dialogTitle" destroyOnClose width="80vw"
            :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <PartOrderDetail :handleType="handleType" :pageType="pageType" :value="formData" @close="closeHandler"/>
    </Drawer>
  </div>
</template>

<script>
import {getPartOrderList, postUpdateStatus, postBatchSubmit} from '@/api/partsmanage/purchaseManage/purchaseSubmit';
import {ddlxArr, ddztArr} from './constant';
import PartOrderDetail from './partOrderDetail';
import OEMImport from './OEMImport';
import batchSubmit from './batchSubmit';
import {dictionary} from '@/utils/dictMixin';
import { getCurrentUser } from '@/api/cms-api/origin/construct/dealer';
export default {
  name: "OEMSubmit",
  components: {
    PartOrderDetail,
    OEMImport,
    batchSubmit
  },
  mixins: [dictionary],
  data() {
    return {
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchApi: getPartOrderList,
      pageType: 'OEM1', //查询页面类型DLR1：经销商采购提报页面 ，DLR2：经销商采购查询， OEM1：总部代提报页面， OEM2：总部采购查询
      fetchParams: {
        xhrAbort: true
      },
      visibleDrawer: false,
      visbleDialog: false,
      handleType: '',
      formData: {},
      loading: false,
      rowSelectData: [],
    };
  },
  computed: {
    dialogTitle() {
      return this.handleType == 'show' ? '配件采购明细' : '配件销售代提报';
    },
    fetchParams2() {
      return {
        pageType: this.pageType,
        ...this.fetchParams
      }
    }
  },
  async created() {
    this.columns.find(x => x.dataIndex === 'xsddsx').hidden = true;
    this.columns.find(x => x.dataIndex === 'xsddsx').dictItems = this.createDictList('5000');
    // 获取当前登录人信息
    await getCurrentUser({}).then(response => {
      console.info(response);
      if (response.data.appId === 'beijing') {
        console.info(this.topFilterList);
        this.topFilterList.find(x => x.fieldName === 'xsddsx').hidden = false;
        this.columns.find(x => x.dataIndex === 'xsddsx').hidden = false;
      }
    });
  },
  methods: {
    createTopFilterList() {
      return [
        {type: 'INPUT', label: '经销商代码', fieldName: 'dealerCodeQ', placeholder: '请输入'},
        {type: 'INPUT', label: '经销商名称', fieldName: 'dealerNameQ', placeholder: '请输入'},
        {type: 'INPUT', label: '销售单号', fieldName: 'xsddhm', placeholder: '请输入'},
        {type: 'SELECT', label: '订单类型', fieldName: 'xsddlx', placeholder: '请选择', itemList: ddlxArr},
        {type: 'SELECT', label: '订单属性', fieldName: 'xsddsx', placeholder: '请选择', hidden: true, itemList: this.createDictList('5000')},
        {
          type: 'RANGE_DATE',
          label: '创建日期',
          fieldName: 'createdAt1|createdAt2',
          valueFormat: 'yyyy-MM-dd',
          placeholder: '请选择'
        },
      ]
    },
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 130,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.detailHandle(props.row, 'edit')}>
                  编辑
                </el-button>
                <el-divider direction="vertical"/>
                <el-button size="mini" type="text" onClick={() => this.detailHandle(props.row, 'show')}>
                  明细
                </el-button>
                <el-divider direction="vertical"/>
                <el-button size="mini" type="text" onClick={() => this.cancleHandle(props.row, 'Q')}>
                  取消
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
        },
        {
          title: '销售单号',
          dataIndex: 'xsddhm',
          width: 150,
          sorter: true,
        },
        {
          title: '订单类型',
          dataIndex: 'xsddlx',
          width: 150,
          sorter: true,
          dictItems: ddlxArr,
        },
        {
          title: '订单属性',
          dataIndex: 'xsddsx',
          width: 150,
          sorter: true,
          dictItems: this.createDictList(5000),
        },
        {
          title: '订单状态',
          dataIndex: 'xsddzt',
          width: 150,
          sorter: true,
          dictItems: ddztArr,
        },
        {
          title: '项目数量',
          dataIndex: 'bszpzs',
          sorter: true,
        },
        {
          title: '采购数量',
          dataIndex: 'bszsls',
          sorter: true,
        },
        {
          title: '采购总金额',
          dataIndex: 'xsysje',
          numberFormat: true,
          sorter: true,
        },
        {
          title: '创建日期',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '驳回原因',
          dataIndex: 'ddbhbz',
        },
      ]
    },
    addHandle() {
      this.handleType = 'add';
      this.visibleDrawer = true
    },
    detailHandle(row, handleType) {
      this.handleType = handleType;
      this.formData = {
        id: row.id,
        data: {
          ...row,
          details: []
        }
      };
      this.visibleDrawer = true
    },
    cancleHandle(row, xsddzt) {
      this.$confirm('您确定要取消吗？取消之后您不可再次操作！', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await postUpdateStatus({
          id: row.id,
          xsddzt
        })
        if (res.resultCode == 200) {
          this.$notify.success({title: '提示信息', message: '取消成功'});
          // 重新加载
          this.fetchParams = Object.assign({}, this.fetchParams);
        }
      }).catch(() => {
      });
    },
    closeHandler(type) {
      this.visibleDrawer = false
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    selectChange(val) {
      this.rowSelectData = val;
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
  }
}
</script>

<style scoped>

</style>
